import os
from pyglet.gl import *
from pyglet import image
from pyglet import font
from primitives import *
import CAConfig

class UICommon:
    def __init__(self, *args, **kwargs):
        self.window = CAConfig.window
        self.font_size_magic_number = self.window.ft.ascent + self.window.ft.descent

class Dialog(UICommon):
    def __init__(self, message="", *args, **kwargs):
        UICommon.__init__(self, *args, **kwargs)
        self.message = ""
        self.text = None
        self.padding = kwargs.get('padding') or 10
        self.height  = kwargs.get('height') or .25
        self.transparency = kwargs.get('transparency') or 0.5
        # Fill up the bottom quarter of the screen with the dialog area
        vertices = [
            (0,0),
            (self.window.viewport[0],0),
            (self.window.viewport[0],int(self.window.viewport[1]*self.height)),
            (0,int(self.window.viewport[1]*self.height))
        ]
        # make it black and see-through
        self.poly = Polygon(vertices,color=(0,0,0,self.transparency))
        self.set_text(message)
        
    def set_text(self, message):
        self.message = message
        self.text = font.Text( self.window.ft, self.message,
                x = self.padding,
                y = int(self.window.viewport[1] * self.height) - self.padding - self.font_size_magic_number)

    def draw(self):
        self.poly.render()
        glColor3f(1,1,1) # reset the colors 
                         # so you don't get transparency everywhere
        self.text.draw()
        
class Collections(UICommon):
    def __init__(self, *args, **kwargs):
        UICommon.__init__(self, *args, **kwargs)
        self.image = pyglet.resource.image('book_small.png')
        # determine the center
        self.x = (self.window.viewport[0] - self.image.width) / 2
        self.y = (self.window.viewport[1] - self.image.height) / 2
    
    def draw(self):
        self.image.blit(self.x,self.y)

class StatusBar(UICommon):
    def __init__(self, *args, **kwargs):
        UICommon.__init__(self, *args, **kwargs)
        self.padding = kwargs.get('padding') or 5
        self.transparency = kwargs.get('transparency') or 0.5
        vertices = [
                (0,self.window.viewport[1]),
                (self.window.viewport[0],self.window.viewport[1]),
                (self.window.viewport[0],self.window.viewport[1] - 28),
                (0,self.window.viewport[1] - 28)
        ]
        # make it black and transparent
        self.poly = Polygon(vertices,color=(0,0,0,self.transparency))
        # cache the hero ref
        self.hero = self.window.hero

    def draw(self):
        self.poly.render()
        health = self.hero.health
        glColor3f(1,1,1) # reset color
        text = font.Text( self.window.ft, 
                "Health: %d/%d" % (health.cur,health.base + health.mod),
                x=10, y=self.window.viewport[1] - self.padding - self.font_size_magic_number)
        text.draw()
